<?php

namespace App\Http\Controllers\Home;

use App\Model\Department;
use App\Model\No_store_station;
use App\Model\Staff;
use App\Model\Station;
use App\Model\Store;
use App\Model\Store_station;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use League\Flysystem\Exception;
//部门管理
class SystermController extends CommonController
{

    //

	protected $department;
	protected $Staff;
	protected $No_store_station;
	protected $Station;
	protected $Store_station;
	public function __construct()
	{
		parent::__construct();
		$this->department=new Department();
		$this->Staff=new Staff();
		$this->No_store_station=new No_store_station();
		$this->Station=new Station();
		$this->Store_station=new Store_station();
	}


//	部门列表
	public function department_list(Request $request){
		$where=[];
		$where['company_id']=$this->company_id;
		$where['status']=isset($request->status)?$request->status:null;
		$where['name']=isset($request->name)?$request->name:null;
		$where['start_time']=isset($request->start_time)?$request->start_time:null;
		$where['end_time']=isset($request->end_time)?$request->end_time:null;
		$where['status_']=isset($request->status_)?$request->status_:null;
		try{
			$data=$this->department->allDepartment($where);
		}catch(\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($data);
		}else{
			return $this->failed($message);
		}

	}
//  部门列表
	public function department_list_(Request $request){
		$data=$this->department->no_store_depart($this->company_id);
		$newArr=[];
		foreach($data as $k=>$v){
			if($v['store'] ==null){
				$newArr[$k]=$v;
		}
		}
		return $this->success($newArr);

	}

//	部门列表
	public function departments(Request $request){
		$company_id=$this->company_id;
		$data=$this->department->departments($company_id);
		return $this->success($data);
	}
//	新增部门
	public function addDepartment(Request $request){
		$this->validate($request,[
			'code'=>'required|unique:department',
			'name' => 'required|unique:department',
			'leader_id'=>'required|Integer',
			'parent_id' => 'required|Integer',
			'is_store' => 'required|Integer',
		]);
		DB::beginTransaction();
		try{
			$data=$request->only($this->department->getFillable());
			$data['company_id']=$this->company_id;
			$result=$this->department->create($data);
			DB::commit();
		}catch(\Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if(!isset($message)){
			return $this->stored($result);
		}else{
			return $this->response($message);
		}

	}

//  编辑部门
	public function department_edit(Request $request){
		try{
			$result=$this->department->where(['id'=>$request->id])->first();
		}catch(\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($result);
		}else{
			return $this->failed($message);
		}

	}


//	编辑保存部门
	public function department_save(Request $request){
		$this->validate($request,[
			'id'=>'required|Integer',
			'code'=>'required|unique:department',
			'name' => 'required|unique:department',
			'leader_id'=>'required|Integer',
			'parent_id' => 'required|Integer',
			'is_store' => 'required|Integer',
		]);
		DB::beginTransaction();
		try{
			$data=$request->only($this->department->getFillable());
			$data['company_id']=$this->company_id;
			$result=$this->department->where(['id'=>$request->id])->update($data);
			DB::commit();
		}catch(\Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if(!isset($message)){
			return $this->updated($result);
		}else{
			return $this->failed($message);
		}

	}

//	新增非门店岗位
	public function add_no_store_station(Request $request){
		$this->validate($request,[
			'station_id'=>'required|Integer',
			'comment' => 'required|String',
			'department_id'=>'required|Integer',
		]);
		$data=$request->input();
		$data['company_id']=$this->company_id;
		$data['name']=$this->Station->where(['id'=>$request->station_id])->first()->name;
		DB::beginTransaction();
		try{
			$result=$this->Store_station->create($data);
			DB::commit();
		}catch (Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if(!isset($message)){
			return $this->stored($result);
		}else{
			return $this->failed($message);
		}

	}

//	非门店岗位列表
	public function no_store_station_list(Request $request){
		$where['department_id']=$request->department_id;
		$where['company_id']=$this->company_id;
		$data=$this->Store_station->no_store_station($where);
		$newArr=[];
		foreach($data as $key=>$value){
			$newArr[$key]['id']=$value['id'];
			$newArr[$key]['name']=$value['name'];
			$newArr[$key]['comment']=$value['comment'];
			$newArr[$key]['create_at']=date('Y-m-d',strtotime($value['created_at']));
		}
		return $this->success($newArr);
	}

//	非门店岗位编辑
	public function edit_no_store_station(Request $request){
		$id=$request->id;
		$data=$this->Store_station->where(['id'=>$id])->first();
		return $this->success($data);
	}

//	非门店岗位编辑保存
	public function save_no_store_station(Request $request){
		$this->validate($request,[
			'id'=>'required|Integer',
			'station_id'=>'required|Integer',
			'comment' => 'required|String',
		]);
		$data=$request->input();
		$data['name']=$this->Station->where(['id'=>$request->station_id])->first()->name;
		DB::beginTransaction();
		try{
			$result=$this->Store_station->where(['id'=>$request->id])->update($data);
			DB::commit();
		}catch (Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if(!isset($message)){
			return $this->success($result);
		}else{
			return $this->failed($message);
		}
	}


//	非门店岗位列表接口
//	public function no_store_station_list_(){
//		$company_id=$this->company_id;
//		$data=$this->Station->where(['company_id'=>$company_id,'is_store'=>2])->select('id','name')->get();
//		return $this->success($data);
//	}

	
}
